﻿Imports System.Globalization
Imports System.Windows.Threading
Imports System.Collections.ObjectModel

Namespace ViewModel

    Public Class LiveProgramViewModel
        Inherits ViewModelBase

        Private LiveProgramModel As Model.LiveProgramModel

        Public Sub New(ByVal model As Model.LiveProgramModel)
            LiveProgramModel = model
        End Sub

        ''' <summary>
        ''' live id
        ''' </summary>
        Public ReadOnly Property Id() As Integer
            Get
                Return LiveProgramModel.Id
            End Get
        End Property

        ''' <summary>
        ''' 放送者名
        ''' </summary>
        ''' <remarks>Nothing のときは未取得</remarks>
        Public Property Caster() As String
            Get
                Return LiveProgramModel.Caster
            End Get
            Set(ByVal value As String)
                LiveProgramModel.Caster = value
                OnPropertyChanged("Caster")
            End Set
        End Property

        Public Property Title() As String
            Get
                Return LiveProgramModel.Title
            End Get
            Set(ByVal value As String)
                LiveProgramModel.Title = value
                OnPropertyChanged("Title")
                OnPropertyChanged("Subset")
            End Set
        End Property

        Private _subset As LiveProgramSubsetViewModel
        Public Property Subset() As LiveProgramSubsetViewModel
            Get
                If _subset Is Nothing Then
                    _subset = New LiveProgramSubsetViewModel(LiveProgramModel)
                End If
                Return _subset
            End Get
            Set(ByVal value As LiveProgramSubsetViewModel)
                _subset = value
                OnPropertyChanged("Subset")
            End Set
        End Property

        ''' <summary>
        ''' 紹介文 全文
        ''' </summary>
        Public Property Content() As String
            Get
                Return LiveProgramModel.Content
            End Get
            Set(ByVal value As String)
                LiveProgramModel.Content = value
                OnPropertyChanged("Content")
            End Set
        End Property

        Public Property ShortContent() As String
            Get
                Return LiveProgramModel.ShortContent
            End Get
            Set(ByVal value As String)
                LiveProgramModel.ShortContent = value
                OnPropertyChanged("ShortContent")
            End Set
        End Property

        Public Property VisitorCount() As Integer
            Get
                Return LiveProgramModel.VisitorCount
            End Get
            Set(ByVal value As Integer)
                LiveProgramModel.VisitorCount = value
                OnPropertyChanged("VisitorCount")
            End Set
        End Property

        Public Property CommentCount() As Integer
            Get
                Return LiveProgramModel.CommentCount
            End Get
            Set(ByVal value As Integer)
                LiveProgramModel.CommentCount = value
                OnPropertyChanged("CommentCount")
            End Set
        End Property

        Public Property IsMemberOnly() As Boolean
            Get
                Return LiveProgramModel.IsMemberOnly
            End Get
            Set(ByVal value As Boolean)
                LiveProgramModel.IsMemberOnly = value
                OnPropertyChanged("IsMemberOnly")
                OnPropertyChanged("Subset")
            End Set
        End Property

        Public Property StartDateTime() As DateTime
            Get
                Return LiveProgramModel.StartDateTime
            End Get
            Set(ByVal value As DateTime)
                LiveProgramModel.StartDateTime = value
                OnPropertyChanged("StartDateTime")
            End Set
        End Property

        Private WithEvents _station As StationViewModel
        Public Property Station() As StationViewModel
            Get
                If _station Is Nothing Then
                    _station = New StationViewModel(LiveProgramModel.Station)
                End If
                Return _station
            End Get
            Set(ByVal value As StationViewModel)
                _station = value
                OnPropertyChanged("Station")
            End Set
        End Property

        Private _icon As IconViewModel
        Public ReadOnly Property Icon() As IconViewModel
            Get
                If _icon Is Nothing Then
                    _icon = New IconViewModel With { _
                        .Icon = Station.Icon}
                End If
                _icon.TargetUri = If(IsOnAir, Uri, Station.Uri)
                Return _icon
            End Get
        End Property

        Public Property OfficialTag() As String
            Get
                Return LiveProgramModel.OfficialTag
            End Get
            Set(ByVal value As String)
                LiveProgramModel.OfficialTag = value
                OnPropertyChanged("OfficialTag")
            End Set
        End Property

        Public Property IsFace() As Boolean
            Get
                Return LiveProgramModel.IsFace
            End Get
            Set(ByVal value As Boolean)
                LiveProgramModel.IsFace = value
                OnPropertyChanged("IsFace")
                OnPropertyChanged("Subset")
            End Set
        End Property

        Public Property CrawlingDateTime() As DateTime
            Get
                Return LiveProgramModel.CrawlingDateTime
            End Get
            Set(ByVal value As DateTime)
                LiveProgramModel.CrawlingDateTime = value
                OnPropertyChanged("CrawlingDateTime")
            End Set
        End Property

        Public ReadOnly Property Uri() As Uri
            Get
                Return New Uri(String.Format(CultureInfo.InvariantCulture, My.Settings.LiveProgramUriFormat, Me.Id))
            End Get
        End Property

        Private _isOnAir As Boolean
        Public Property IsOnAir() As Boolean
            Get
                Return _isOnAir
            End Get
            Set(ByVal value As Boolean)
                _isOnAir = value
                Me.HasRecentInformation = True
                OnPropertyChanged("IsOnAir")
            End Set
        End Property

        Private _isAlerted As Boolean
        Public Property IsAlerted() As Boolean
            Get
                Return _isAlerted
            End Get
            Set(ByVal value As Boolean)
                _isAlerted = value
                OnPropertyChanged("IsAlerted")
            End Set
        End Property

        ' 通知対象時、前回の放送情報をもっているか
        Private _hasRecentInformation As Boolean
        Public Property HasRecentInformation() As Boolean
            Get
                Return _hasRecentInformation
            End Get
            Set(ByVal value As Boolean)
                _hasRecentInformation = value
                OnPropertyChanged("HasRecentInformation")
            End Set
        End Property

        Private Sub _station_PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Handles _station.PropertyChanged
            If e.PropertyName = "Icon" Then
                _icon.Dispose()
                _icon = Nothing
                OnPropertyChanged("Icon")
            End If
        End Sub

        ' For Xceed DataGrid
        Private _group As String = ""
        Public Property Group() As String
            Get
                Return _group
            End Get
            Set(ByVal value As String)
                _group = value
            End Set
        End Property

        Private WithEvents _tags As New ObservableCollection(Of TagViewModel)
        Public ReadOnly Property Tags() As ObservableCollection(Of TagViewModel)
            Get
                Return _tags
            End Get
        End Property


        Public ReadOnly Property FilterdTags() As IList(Of String)
            Get
                Dim tags = From t In Me.Tags Order By t.Kind = TagKind.Caster, t.Kind = TagKind.User, t.Kind = TagKind.Self _
                           Select t.Value Distinct

                Return tags.ToArray
            End Get
        End Property

        Private Sub _tags_CollectionChanged(ByVal sender As Object, ByVal e As System.Collections.Specialized.NotifyCollectionChangedEventArgs) Handles _tags.CollectionChanged
            OnPropertyChanged("FilterdTags")
        End Sub


    End Class
End Namespace
